home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cc02.zip / CSORT1.C < prev    next >
Text File  |  1984-08-19  |  1KB  |  81 lines

  1. /* -- csort.c  sorting benchmark -- */
  2. /* ---- calls random the number of times specified by MAXNUM to create an
  3.     array of lon integers, then does a quicksort on the array of longs.
  4.     The program does this for the number of times specified by COUNT.
  5.    ---- */
  6.  
  7. #include "stdio.h"
  8.  
  9. #define MAXNUM 1000
  10. #define COUNT 10
  11. #define MODULUS ((long) 0x20000)
  12. #define C 13849L
  13. #define A 25173L
  14.  
  15. long seed = 7L;
  16. long random();
  17. long buffer[MAXNUM] ={0};
  18.  
  19. main() {
  20.     int i,j;
  21.     long temp;
  22.  
  23.     printf("Filling array and sorting %d times\n", COUNT);
  24.     for (i = 0; i < COUNT; ++i) {
  25.         for (j = 0; j < MAXNUM; ++j) {
  26.             temp = random(MODULUS);
  27.             if (temp < 0L)
  28.                 temp = (-temp);
  29.             buffer[j] = temp;
  30.         }
  31.         printf("Buffer full, iteration %d\n", i);
  32.         quick(0, MAXNUM, buffer);
  33.     }
  34.     printf("Done\n");
  35. }
  36.  
  37. quick(lo, hi, base)
  38.  
  39.     int lo, hi;
  40.     long base[];
  41.  
  42.     {
  43.         int i, j;
  44.         long pivot, temp;
  45.  
  46.         if (lo < hi) {
  47.             for (i = lo, j = hi, pivot = base[hi]; i < j; ) {
  48.                 while (i < j && base[i] < pivot)
  49.                     ++i;
  50.                 while (j > i && base[j] > pivot)
  51.                     --j;
  52.                 if (i < j) {
  53.                     temp = base[i];
  54.                     base[i] = base[j];
  55.                     base[j] = temp;
  56.                 }
  57.             }
  58.             temp = base[i];
  59.             base[i] = base[hi];
  60.             base[hi] = temp;
  61.             quick (lo, i - 1, base);
  62.             quick (i + 1, hi, base);
  63.         }
  64.     }
  65.  
  66. long random(size)
  67.  
  68.     long size;
  69.  
  70.     {
  71.         seed = seed * A + C;
  72.         return (seed % size);
  73.     }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.